package com.myhome.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.myhome.config.constant.R;
import com.myhome.domain.Sms;
import com.myhome.domain.User;
import com.myhome.service.EmailService;
import com.myhome.service.SmsService;
import com.myhome.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * @Author QYiXin
 * @Date 2021 10 16 14 31
 **/
@RestController
@Api(tags = "短信控制器")
@RequestMapping("/sms")
public class SmsController {
    @Autowired
    SmsService smsService;
    @Autowired
    EmailService emailService;
    @Autowired
    UserService userService;
    @GetMapping("/sendToNo")
    @ApiOperation(value = "发送短信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sms", value = "短信的json数据")
    })
    public R sentSmsTo(@RequestBody Sms sms){
        Boolean isSend = smsService.sendMessage(sms);
        if (isSend) {
            return R.ok();
        }
        return R.fail("发送失败");
    }

    @PostMapping("/sendTo")
    @ApiOperation(value = "发送短信")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sms", value = "短信的json数据")
    })
    public R sendEmail(@RequestBody Sms sms) {
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getMobile, sms.getMobile()));
        if (user != null) {
            sms.setEmail(user.getEmail());
            String mobile = sms.getMobile();
            Boolean isSend = emailService.sendEmail(sms);
            if (isSend) {
                return R.ok();
            }
            return R.fail("发送失败");
        }
        return R.fail("无该用户");
    }

}
